package com.huicoo.forestmanager.ui.web;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.KeyEvent;

import com.huicoo.forestmanager.R;
import com.huicoo.forestmanager.base.BaseActivity;


/**
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2019/4/12	    Administrator
 * 通用的 web activity
 * 接收参数：
 * String url           跳转链接
 */
public class CustomWebActivity extends BaseActivity {
    private CustomWebFragment fragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_custom_web;
    }

    @Override
    protected void initView() {
        Bundle initBundle = getIntent().getExtras();
        if (initBundle != null) {
            String url = initBundle.getString("url", "");
            if (!TextUtils.isEmpty(url)) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                fragment = CustomWebFragment.getInstance(url);
                ft.add(R.id.webActivityContainer, fragment);
                ft.show(fragment);
                ft.commitAllowingStateLoss();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (fragment != null) {
                fragment.onActivityResult(requestCode, resultCode, data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (fragment != null) {
                boolean isDealBack = fragment.dealWebBack();
                if (isDealBack) {
                    return true;
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
